// pages/device/defingerCheck/defingerCheck.js
import lwx from '../../../utils/lwx.js'
import {
  _globle
} from '../../../utils/globle.js'
import devices from '../../../utils/devices.js'

Page({
  _useId: 5,
  rand2: 0,
  rand1: 0,
  /**
   * 页面的初始数据
   */
  data: {
    devicesCode: '',
    devices: {},
    devicesInfo: {},
    status: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      devicesCode: options.devicesCode
    })
    this.loadData();
  },

  loadDevice: function(status) {
    if (!this._devices) {
      this._devices = new devices(this.data.devicesCode)
    }
    wx.showLoading({
      title: '读取设备信息',
      mask: true
    })
    this._devices.getDevicesInfo().then(res => {
      console.log(res)
      this.setData({
        devicesInfo: res
      })
      let initialised = res.initialised
      wx.hideLoading()
      wx.showLoading({
        title: '读取状态信息',
        mask: true
      })
      return this._devices.getOperationStatus()
    }).then(res => {
      console.log(res)
      if (res.operationStatus == 2) {
        this.setData({
          status: 3
        })
      }
      wx.hideLoading()
      this.rand2 = res.rand2;
      this.rand1 = res.rand1;
      return this._devices.setOperationCommand({
        type: status,
        sign: this.data.devices.sign,
        rand1: res.rand1,
        rand2: res.rand2
      })
    }).then(res => {
      let statusInterval = setInterval(() => {
        this._devices.getOperationStatus().then(res => {
          if (res.operationStatus !== 1) {
            clearInterval(statusInterval)
            wx.hideLoading()
            console.log('状态**********====' + res);
            if (this.data.status == 3){
              this.setData({
                status: 1
              })
            }else{
              this.setData({
                status: 2
              })
            }
            // resolve(res.operationStatus)
          } else if (res.operationStatus == 2) {
            wx.hideLoading()
            clearInterval(statusInterval)
            this.setData({
              status: 3
            })
          }
        })
      }, 100)
    }).then(res => {
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: '读取数据失败',
        mask: true,
        icon: 'none'
      })
    })
  },


  releaseError: function() {
    if (!this._devices) {
      this._devices = new devices(this.data.devicesCode)
    }
    wx.hideLoading()
    wx.showLoading({
      title: '读取状态...',
      mask: true
    })
    this._devices.getOperationStatus().then(res => {
      console.log('进来解锁')
      if (res.operationStatus === 2) {
        wx.hideLoading()
        wx.showLoading({
          title: '解除锁定',
          mask: true
        })
        this._devices.setOperationCommand({
          type: 3,
          sign: this.data.detail.sign,
          rand1: res.rand1,
          rand2: res.rand2
        }).then(res => {
          return new Promise((resolve, reject) => {
            console.log('解锁结果')
            wx.hideLoading()
            wx.showLoading({
              title: '获取结果...',
              mask: true
            })
            // 循环读取状态
            let statusInterval = setInterval(() => {
              this._devices.getOperationStatus().then(res => {
                // 空闲则是安装完成
                if (res.operationStatus !== 1) {
                  console.log('解锁成功')
                  clearInterval(statusInterval)
                  resolve(res.operationStatus)
                }
              })
            }, 100)
          })
        }).then(res => {
          wx.hideLoading()
          if (res === 2) {
            wx.showToast({
              title: '解除错误锁定失败',
              mask: true,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '解除成功',
              mask: true
            })
          }
        }).catch(err => {
          console.log(err)
          wx.hideLoading()
          wx.showToast({
            title: '解除操作失败',
            mask: true,
            icon: 'none'
          })
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          title: '设备正常，无错误信息',
          mask: true,
          icon: 'none'
        })
      }
    }).catch(res => {
      wx.hideLoading()
      wx.showToast({
        title: '获取运行状态失败',
        mask: true,
        icon: 'none'
      })
    })
  },
  loadData: function() {
    var that = this;
    lwx.request({
      url: "devicesInfo.info",
      data: {
        devicesCode: that.data.devicesCode
      }
    }).then(res => {
      console.log(res)
      if (res.data.code == '0') {
        that.setData({
          devices: res.data.devices
        })
      }
    }).catch(err => {
      console.log('err' + err);
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (this._devices) {
      console.log('断开连接')
      this._devices.closeConnection()
    }
  },

  pass: function() {
    this._devices.closeConnection()
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  agree: function() {
    this.loadDevice(8);
  },
  check: function() {
    this.loadDevice(11);
  },
  unlock: function() {
    this.loadDevice(3);
    this.setData({
      status:1
    })
  }
})